package com.example.api.service.impl;

import com.example.api.constant.StatusConst;
import com.example.api.dto.CustomerDTO;
import com.example.api.mapper.CustomerMapper;
import com.example.api.mapper.UserMapper;
import com.example.api.pojo.Customer;
import com.example.api.pojo.User;
import com.example.api.service.CustomerService;
import com.example.api.vo.CustomerVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Value("${example.password}")
    private String password;

    @Value("${example.salt}")
    private String salt;


    @Resource
    CustomerMapper customerMapper;

    @Override
    public CustomerVO login(String phone, String password) {
        password = DigestUtils.md5DigestAsHex((password+salt).getBytes());
        return customerMapper.login(phone,password);
    }

    @Override
    public void add(CustomerDTO customerDTO) {
        Customer customer = new Customer();
        BeanUtils.copyProperties(customerDTO,customer);

        customer.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        customer.setCreateTime(new Date());

        customerMapper.add(customer);
    }

    @Override
    public Customer findCustomerById(Long customerId) {
        return customerMapper.findCustomerByTd(customerId);
    }
}
